<?php 

defined('BASEPATH') OR exit('No direct script access allowed');
class Mod_book extends CI_Model{
	function book(){
		parent::__construct();	
	}
	function getBook(){
		$query = $this->db->get("tbl_posting");
		return $query->result_array();
	}
	function getAdmin($username,$password){
		$this->db->select("*");
		$this->db->from("tbl_users");
		$this->db->where("tbl_users.username",$username);
		$this->db->where("tbl_users.password",$password);
		$this->db->limit(1);
		$query = $this->db->get();
        if($query->num_rows() == 1) { 
            return $query->row(); 
        } else {
            return false; 
        }
	}
	function getUserProfile($email,$password){
		$this->db->select("*");
		$this->db->from("tbl_users");
		$this->db->where("email",$email);
		$this->db->where("password",md5($password));
		$this->db->limit(1);
		$query = $this->db->get();
        if($query->num_rows() == 1) { 
            return $query->row(); 
        } else {
            return false; 
        }
	}
	function addRegisterEmail($email,$agree){
		$this->db->set("email",$email);
		$this->db->set("agree",$agree);
		$this->db->insert("tbl_users");
		return $this->db->insert_id();
	}

	function addNewEmailAddress($arr){
		
		$this->db->insert("tbl_users",$arr);
		
	}
	function checkOrderExit($email){
		$this->db->select("*");
		$this->db->from("tbl_user_shipping");
		$this->db->where("tbl_user_shipping.userId",$email);
		$this->db->join("tbl_country","tbl_user_shipping.countryid=tbl_country.countryid");
		$this->db->limit(1);
		return $this->db->get();
	}
	function getAllShippingAddress($userId){
		$this->db->select("*");
		$this->db->from("tbl_user_shipping");
		$this->db->where("tbl_user_shipping.userId",$userId);
		$this->db->join("tbl_shipping","tbl_user_shipping.shippingID=tbl_shipping.shippingID");
		$this->db->join("tbl_country","tbl_user_shipping.countryid=tbl_country.countryid");
		return $this->db->get();
	}
	function updateShippingAddess($id,$arr){
		$this->db->where("user_shipping_id",$id);
		$this->db->update("tbl_user_shipping",$arr);
	}
	function addNewShippingAddess($arr){
		//$this->db->where("orderUId",$id);
		$this->db->insert("tbl_user_shipping",$arr);
	}
	function deleteDetailShipping($id){
		$this->db->where("user_shipping_id",$id);
		$this->db->delete("tbl_user_shipping");
	}

	function getDetailShipping($id){
		$this->db->select("*");
		$this->db->from("tbl_user_shipping");
		$this->db->where("tbl_user_shipping.user_shipping_id",$id);
		//$this->db->join("tbl_country","tbl_user_shipping.countryid=tbl_country.countryid");
		$this->db->limit(1);
		return $this->db->get();
	}

	function addOrderInfo($name,$country,$street,$city,$province,$sipCode,$mobile,$apartment,$postOffice){
		$arr = array(
			"fullname"=>$name,
			"countryid"=>$country,
			"street"=>$street,
			"city"=>$city,
			"state"=>$province,
			"zipCode"=>$sipCode,
			"mobile"=>$mobile,
			"apartment"=>$apartment,
			"userId"=>$this->session->userdata("userId"),
			"email"=>$this->session->userdata("email"),
			"shippingID"=>$postOffice
		);
		$insert = $this->db->insert("tbl_user_shipping",$arr);
		return $this->db->insert_id();
	}

	function getShippingAddressPrice($idUser){
		$this->db->select("shipping_price");
		$this->db->from("tbl_user_shipping");
		$this->db->where("tbl_user_shipping.user_shipping_id",$idUser);
		$this->db->join("tbl_shipping","tbl_shipping.shippingID = tbl_user_shipping.shippingID");
		return $this->db->get();
	}

	function getShippingPrice(){
		return $this->db->get("tbl_shipping");
	}
	function checkZipExit($ship_code){
		$this->db->where("tbl_user_shipping.zipCode",$ship_code);
		$this->db->where("tbl_user_shipping.userId",$this->session->userdata("userId"));
		$result = $this->db->get("tbl_user_shipping");
		if($result->num_rows() <= 0){
			return false;

		}else{
			return $result->row();

		}
	}
	function addOrderBook($data){
		$this->db->insert_batch("tbl_order_detail",$data);
		
		//return $this->db->insert_id();

	}
	function payOrderBook($data){
		$this->db->set("pay",1);
		$this->db->where_in("order_detail_id",$data);
		$this->db->update("tbl_order_detail");
	}
	function addOrderBookAll($data){
		$this->db->insert("tbl_order_detail",$data);
		
		return $this->db->insert_id();
	}
	function getCountry(){
		return $this->db->get("tbl_country");
	}

	function checkMailExit($mail){
		$this->db->where("email",$mail);
		return $this->db->get("tbl_users");
	}

	function getDetailBook($id){
		$this->db->select("*");
		$this->db->from("tbl_posting");
		$this->db->where("tbl_posting.postid",$id);
		$this->db->limit(1);
		return $this->db->get();
	}

	function addBook($name,$lable,$detial,$price,$imgs,$shipping){
		$arr = array(
			"date_post"=>date('Y-m-d'),
			"post_title"=>$name,
			"post_description"=>$detial,
			"label"=>$lable,
			"price"=>$price,
			"post_image"=>$imgs,
			"shipping"=>$shipping
		);
		$this->db->insert("tbl_posting",$arr);
	}

 // Thymeak
	function viewpost(){
		$this->db->select("*");
		$this->db->from("tbl_posting");        
	    return $this->db->get();
	}

	function deletepost($postid){
		$this->db->where("postid",$postid);
		$this->db->delete("tbl_posting");		
	}


	function getuser($username,$password){
		$this->db->select("*");	
		$this->db->from("tbl_users");	
		$this->db->where('username',$username);
		$this->db->where('password',$password);

		$this->db->get();
	}



	function form_signin($username,$password){
		$this->db->select("*");	
		$this->db->from("tbl_users");	
		$this->db->where('email',$username);
		$this->db->where('password',$password);
		$this->db->get();
	}

	function deleteuserbymail($user_mail){
		$this->db->where("email",$user_mail);
		$this->db->delete("tbl_users");		
	}

	function addActiveEmail($userid){

		$data = $arrayName = array('active' =>'1');
		$this->db->where('userid',$userid);
		$this->db->update('tbl_users',$data);

	}

	function addRegister_info($data,$userid){
		$this->db->where('userid',$userid);
		$this->db->update('tbl_users',$data);
	}
	function getACountry($country){
		$this->db->where("tbl_country.countryid",$country);
		$this->db->limit(1);
		return $this->db->get("tbl_country");
	}


	function addOrderuser($data){

		$insert = $this->db->insert("tbl_order_user",$data);
		return $this->db->insert_id();
	}


 // Thymeak
}